﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Areas.Administrator.Models;

namespace CollegeWebsite.Areas.Administrator.Controllers
{
    public class FacilityController : Controller
    {
        readonly CollegeWebsiteDataContext _dataContext = new CollegeWebsiteDataContext();
        //
        // GET: /Administrator/Facility/

        public ActionResult Index()
        {
            List<Facility> listFacility = _dataContext.Facilities.Select(f => f).ToList();
            return View(listFacility);
        }

        //
        // GET: /Administrator/Facility/Details/5

        public ActionResult Details(int id)
        {
            Facility facility = _dataContext.Facilities.Select(f => f).Single(f => (f.FacilityID == id));
            return View(facility);
        }

        //
        // GET: /Administrator/Facility/Create

        public ActionResult Create()
        {
            Facility facility = new Facility();           
            return View(facility);
        } 

        //
        // POST: /Administrator/Facility/Create

        [HttpPost, ValidateInput(false)]
        public ActionResult Create(Facility facility)
        {
            try
            {
                // TODO: Add insert logic here
                _dataContext.Facilities.InsertOnSubmit(facility);
                _dataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Administrator/Facility/Edit/5
 
        public ActionResult Edit(int id)
        {
            Facility facility = _dataContext.Facilities.Single(f => (f.FacilityID == id));
            return View(facility);
        }

        //
        // POST: /Administrator/Facility/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(int id, Facility newFacility)
        {
            try
            {
                // TODO: Add update logic here
                Facility oldFacility = _dataContext.Facilities.Single(f => f.FacilityID == id);
                oldFacility.FacilityName = newFacility.FacilityName;
                oldFacility.FacilityDescription = newFacility.FacilityDescription;
                oldFacility.FacilityCapicity = newFacility.FacilityCapicity;
                oldFacility.FacilityFee = newFacility.FacilityFee;
                _dataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Administrator/Facility/Delete/5
 
        public ActionResult Delete(int id)
        {
            Facility facility = _dataContext.Facilities.Single(f => f.FacilityID == id);
            return View(facility);
        }

        //
        // POST: /Administrator/Facility/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                Facility facility = _dataContext.Facilities.Single(f => f.FacilityID == id);
                _dataContext.Facilities.DeleteOnSubmit(facility);
                _dataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch(Exception exception)
            {
                ViewData["ErrorInfo"] = exception.Message;
                return View("Error");
            }
        }
    }
}
